var express = require("express"); //导入express  （下载）
var app = express();
var bodyParser = require("body-parser"); //导入body-parser  （下载）
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));
 
// 跨域
var allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*'); //自定义中间件，设置跨域需要的响应头。
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); //允许任何方法
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,X-Session-Token'); //允许任何类型
    next();
};
app.use(allowCrossDomain);
 
 
 
 
dataInfo = [
    { name: "苹果", count: 0, price: 10.01, check: false, },
    { name: "桃子", count: 0, price: 22.11, check: false, },
    { name: "橘子", count: 0, price: 8.12, check: false, },
]
//http://127.0.0.1:3000/list/fruitsData
app.get('/list/fruitsData', (req, res) => {
    res.status(200),
        res.send({ code: 200, describe: "success", data: dataInfo })
})
 
//http://127.0.0.1:3000/user/login
app.post('/user/login', (req, res) => {
    var { username, password } = req.body
    if (username != "xq" || password != "xq123") {
        return res.send({ code: 404, describe: "用户名或密码不正确" })
    } else {
        return res.send({ code: 200, describe: "success", data: { username, password } })
    }
})
 
 
 
const port = process.env.PORT || 3000
const host = process.env.HOST || '127.0.0.1'
app.listen(port, host, () => {
    console.log(`Server is running on http://127.0.0.1:3000`);
});